package com.f21.f21assessoria.util;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

public class UtilMessage {

	private static final String PATH_MENSAGENS = "/mensagens";

	public static String getBundleMessage(String codigo) {
		return ResourceBundle.getBundle(PATH_MENSAGENS).getString(codigo);
	}

	public static String getBundleMessage(String codigo, String[] param) {
		String msg = ResourceBundle.getBundle(PATH_MENSAGENS).getString(codigo);
		msg = MessageFormat.format(msg, param);
		return msg;
	}

	/**
	 * 
	 * @param msg
	 *            - mensagem de erro
	 * @return FacesMessage com mensagem de erro passada
	 */
	public static FacesMessage getErroMassage(String msg) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				msg, msg);
		return message;
	}

	// ResourceBundle.getBundle(
	public static FacesMessage getErroMessageBundle(String cod) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				getBundleMessage(cod), getBundleMessage(cod));
		return message;
	}
	
	public static FacesMessage getErroMessageBundle(String cod, String[] param) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				getBundleMessage(cod, param), getBundleMessage(cod, param));
		return message;
	}

	public static FacesMessage getInfoMessageBundle(String cod) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				getBundleMessage(cod), getBundleMessage(cod));
		return message;
	}

	public static void addInfoMessageBundle(String id,String cod) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				getBundleMessage(cod), getBundleMessage(cod));
		FacesContext.getCurrentInstance().addMessage(id, message);
	}
	
	public static void addInfoMessageBundle(String cod) {
		addInfoMessageBundle(null,cod);
	}
	

	public static void addInfoMessageBundle(String cod, String parm[]) {
		addInfoMessageBundle(null, cod, parm);
	}
	
	public static void addInfoMessageBundle(String id,String cod, String parm[]) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				getBundleMessage(cod,parm), getBundleMessage(cod,parm));
		FacesContext.getCurrentInstance().addMessage(id, message);
	}

}
